package com.huanying.goodjob.prize;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.huanying.framework.BaseController;
import com.huanying.framework.PageBean;

@Controller
public class PrizeController extends BaseController{
	@Autowired
	private PrizeService prizeService;
	private static Logger logger = Logger.getLogger(PrizeController.class);
	//查询奖品
	@RequestMapping("/show_add_prize.do")
	public String show_add_prize_chance(String name,String chance,String price,String page_num,Model model){
		
		Map<String,Object> map=new HashMap<String,Object>();
		PageBean pageBean = null;
		map.put("name", name);
		map.put("chance",chance);
		map.put("price", price);
		if(page_num==null)
		{
			page_num="1";
		}
		try {
			pageBean = prizeService.search_prize(map, super.getPageSize(), Integer.valueOf(page_num));
		} catch (NumberFormatException e) {
			logger.error("查询奖品失败", e);
			// TODO Auto-generated catch block
		} catch (Exception e) {
			logger.error("查询奖品失败", e);
			// TODO Auto-generated catch block
		}
		model.addAttribute("name",name);
		model.addAttribute("chance",chance);
		model.addAttribute("price",price);
		model.addAttribute("prizes",pageBean.getList());
		model.addAttribute("page",pageBean);
		return "/prize/add_prize";
	}
	//删除奖品
	@RequestMapping("/delete_prize.do")
	@ResponseBody
	public Map<String,Object> delete_prize_chance(String id){
		Map<String,Object> map=new HashMap<String,Object>();
		try {
			prizeService.delete_prize(Integer.parseInt(id));
			map.put("flag", "1");
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			logger.error("删除奖品记录失败", e);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.error("删除奖品记录失败", e);
		}
		return map;
	}
	//添加奖品
	@RequestMapping("/add_prize.do")
	@ResponseBody
	public Map<String,String> add_prize(@RequestBody Prize prize,String status)
	{
		Map<String,String> map=new HashMap<String,String>();
	    try {
	    	if(prize.getId()==0)
	    	{
	    		prize.setStatus(1);
			prizeService.add_prize(prize);
	    	}else{
	    		prize.setStatus(Integer.parseInt(status));
	    		prizeService.update_prize(prize);
	    	}
		    map.put("flag", "1");
		    map.put("id",Integer.toString(prize.getId()));
	    } catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			logger.error("添加奖品失败", e);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.error("添加奖品失败", e);
		}
	   return map;
	}
	//获得单个奖品
	@RequestMapping("/get_prize.do")
	@ResponseBody
	public Map<String,Object> get_prize(String id)
	{
		Map<String,Object> map=new HashMap<String,Object>();
		Prize prize;
		try {
			prize=prizeService.get_prize(Integer.parseInt(id));
			map.put("prize", prize);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.error("获取单个奖品失败", e);
		}
		return map;
	}
	

}
